import axios from 'axios'
import store from '@/store'
import router from '@/router'
import { message } from 'ant-design-vue'

export const baseUrl = '//zhuli.test.checkurl.cn'

const request = axios.create({
  baseURL: baseUrl,
  timeout: 0
})

// 请求拦截
request.interceptors.request.use(config => {
  localStorage.getItem('userToken') ? config.headers['token'] = localStorage.getItem('userToken') : ''
  config.cancelToken = new axios.CancelToken(cancel => store.commit('addHttpRequestList', cancel))
  return config
}, err => {
  err.response = { code: 500, msg:'请求失败，请检查网络。' }
  return err.response
})

// 响应拦截
request.interceptors.response.use(response => {
  return response.data
}, err => {
  if(err.message === 'suspend') {
    return new Promise(() => {})
  }else if (err.message.indexOf('timeout') !== -1) {
    err.response = { code: 500, msg:'响应超时，请检查网络。' }
    return err.response
  }else if(err.message.indexOf('code 400') !== -1){
    if(!store.state.frontRoute.includes(window.location.pathname)){
      message.error('您未登录或已超时，请先登录后，再进行操作。')
      router.replace('/')
    }
    localStorage.removeItem('userToken')
    store.commit('changeState', ['userInfo', {}])
    store.commit('changeState', ['isLogin',false])
    return false
  }else{
    message.error('请求失败，请检查网络。')
    return Promise.reject(err)
  }
})

export default request

// 网页预览请求
export function pagePreview(url,formData){
  return request.get(url,formData)
}
// 网页预览ua请求
export function uaRequest(url){
  return request.get(url)
}
// 获取网站信息
export function trackIndex(formData){
  return request.get('/track_index',{params:formData})
}
// 滑块验签
export function wall(formData){
  return request.post('/wall',formData)
}
// 发送验证码
export function sendCode(formData){
  return request.post('/api/send_code',formData)
}
// 校验验证码
export function verifyYzm(formData){
  return request.post('/api/verify_yzm',formData)
}
// 注册
export function registerApi(formData){
  return request.post('/api/register',formData)
}
// 登录
export function loginApi(formData){
  return request.post('/api/home_login',formData)
}
// 退出登录
export function loginout(){
  return request.post('/api/logout')
}
// 忘记或修改密码
export function forgetPwd(formData){
  return request.post('/api/forget_pwd',formData)
}
// 修改手机号
export function changeMobile(formData){
  return request.post('/change_mobile',formData)
}
// 上传图片
export function uploadImg(formData){
  return request.post('/upload_img',formData)
}
// 修改图片
export function changeImg(formData){
  return request.post('/change_portrait',formData)
}
// 修改昵称
export function changeNickname(formData){
  return request.post('/change_nickname',formData)
}
// 微信
export function getWechat(){
  return request.get('/api/wechat')
}
// 验证微信是否登录
export function getStatus(formData){
  return request.get('/api/status',{ params: formData })
}
// 微信登录 绑定手机号
export function checkMobile(formData){
  return request.post('/check_mobile',formData)
}
// 获取用户信息
export function getUser(){
  return request.post('/get_user_tofront')
}
// 绑定微信
export function bindWechat(){
  return request.post('/bind_wechat')
}
// 验证是否成功绑定微信
export function changStatus(formData){
  return request.get('/api/change_status',{ params:formData })
}
// 解绑微信
export function unboundWechat(){
  return request.get('/unbound_wechat')
}
// 高频链接
export function highUrl(){
  return request.get('/api/high_url')
}
// 获取通知
export function getHomeNotice(){
  return request.get('/get_home_notice')
}
// 点击已读
export function getNoticeById(formData){
  return request.get('/get_notice_by_id',{ params: formData })
}
// 关闭通知
export function forkNoticeById(formData){
  return request.get('/fork_notice_by_id',{ params: formData })
}
//注册判断手机号是否存在
export function mobileIsReg(formData){
  return request.post('/api/mobile_is_reg',formData)
}
// 新手
export function getSkill(){
  return request.get('/skill_get')
}
// 常见问题
export function getPro(){
  return request.get('/pro_get')
}
// 技巧
export function getUse(){
  return request.get('/use_get')
}


